package com.android.settings.audio.peq.peq;

import android.graphics.PointF;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by We on 2023/3/18
 */
public class Dataset {

    /**
     * 通过给定的点(包含起始点,若干控制点, 结束点)创建bezier 点集,
     *
     * @param bezierPoints
     * @return
     */
    public static List<PointF> buildBezierPoints(List<PointF> bezierPoints) {
        List<PointF> points = new ArrayList<>();
        int order = bezierPoints.size() - 1;
        final int FRAME = 1000;
        float delta = 1f / FRAME;
        for (float t = 0; t <= 1; t += delta) {
            points.add(new PointF(deCasteljauX(bezierPoints, order, 0, t), deCasteljauY(bezierPoints, order, 0, t)));
        }
        return points;
    }

    /**
     * deCasteljau算法
     *
     * @param i 阶数
     * @param j 点
     * @param t 时间
     * @return
     */
    public static float deCasteljauX(List<PointF> bezierPoints, int i, int j, float t) {
        if (i == 1) {
            return (1 - t) * bezierPoints.get(j).x + t * bezierPoints.get(j + 1).x;
        }
        return (1 - t) * deCasteljauX(bezierPoints, i - 1, j, t) + t * deCasteljauX(bezierPoints, i - 1, j + 1, t);
    }

    /**
     * deCasteljau算法
     *
     * @param i 阶数
     * @param j 点
     * @param t 时间
     * @return
     */
    public static float deCasteljauY(List<PointF> controlPoints, int i, int j, float t) {
        if (i == 1) {
            return (1 - t) * controlPoints.get(j).y + t * controlPoints.get(j + 1).y;
        }
        return (1 - t) * deCasteljauY(controlPoints, i - 1, j, t) + t * deCasteljauY(controlPoints, i - 1, j + 1, t);
    }
}
